package com.bit.book.controller;

import com.bit.book.constant.Constants;
import com.bit.book.model.BookInfo;
import com.bit.book.model.PageRequest;
import com.bit.book.model.PageResult;
import com.bit.book.model.Result;
import com.bit.book.service.BookService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RequestMapping("/book")
@RestController
public class BookController {
    @Autowired
    private BookService bookService;

    @RequestMapping(value = "/addBook", produces = "application/json")
    public String addBook(BookInfo bookInfo) {
        log.info("添加图书, bookInfo: {}", bookInfo);
        if(bookInfo == null
        || !StringUtils.hasLength(bookInfo.getBookName())
        || !StringUtils.hasLength(bookInfo.getAuthor())
        || !StringUtils.hasLength(bookInfo.getPublish())
        || bookInfo.getStatus() == null
        || bookInfo.getCount() == null
        || bookInfo.getPrice() == null) {
            log.error("添加图书参数校验失败, bookInfo: {}", bookInfo);
            return "参数校验失败";
        }
        try{
            Integer result = bookService.addBook(bookInfo);
            if(result == 1) {
                return "";
            }

        }catch (Exception e) {
            log.error("添加图书异常，e:", e);
            return "内部错误，请联系管理员";
        }
        return "内部错误，请联系管理员";

    }

    @RequestMapping("/getBookListByPage")
    public Result<PageResult<BookInfo>> getBookListByPage(PageRequest pageRequest, HttpSession session){
        log.info("分页查询图书，pageRequest: {}", pageRequest);
//        //判断用户是否登录
//        if(!StringUtils.hasLength((String)session.getAttribute(Constants.SESSION_LOGIN_USER_NAME))){
//            return Result.unLogin();
//        }

        return Result.success(bookService.getListByPage(pageRequest));
    }

    @RequestMapping("/queryBookById")
    public BookInfo queryBookById(Integer bookId){
        log.info("根据id查询图书，bookId: {}", bookId);
        return bookService.queryBookById(bookId);
    }

    @RequestMapping(value = "/updateBookById", produces = "application/json")
    public String updateBookById(BookInfo bookInfo){
        log.info("更新图书信息，bookId:{}",bookInfo);
        try{
            Integer result = bookService.updateBookById(bookInfo);
            if(result < 1){
                return "内部错误，请联系管理员";
            }
            return "";
        }catch(Exception e){
            log.error("更新图书信息异常，e:",e);
            return "内部错误，请联系管理员";
        }
    }

    @RequestMapping(value = "/deleteBook", produces = "application/json")
    public String deleteBook(Integer id){
        log.info("删除图书，bookId:{}",id);
        try{
            Integer result = bookService.deleteBookById(id);
            if(result < 1){
                return "内部错误，请联系管理员";
            }
            return "";
        }catch(Exception e){
            log.error("删除图书异常，e:",e);
            return "内部错误，请联系管理员";
        }
    }

    @RequestMapping(value = "/batchDeleteBook", produces = "application/json")
    public String batchDeleteBook(@RequestParam List<Integer> ids){
        log.info("批量删除图书，ids:{}",ids);
        try{
            Integer result = bookService.batchDeleteBook(ids);
            if(result < 1){
                return "内部错误，请联系管理员";
            }
            return "";
        }catch(Exception e){
            log.error("批量删除图书异常，e:",e);
            return "内部错误，请联系管理员";
        }

    }

}


